package _04Dom4j;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;

public class DemoDom4j {
    public static void main(String[] args) throws DocumentException {
        //创建解析器
        SAXReader reader = new SAXReader();

        //通过解析器获得文档对象
        Document document = reader.read("day14\\src\\_04Dom4j\\contact.xml");

        //解析文档
        //获得根元素
        Element rootElement = document.getRootElement();
        System.out.println("rootElement = " + rootElement.getName());

        //获取所有子元素
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            System.out.println("element.getName() = " + element.getName());
        }
        System.out.println("==========================");
        //获得一个子元素
        Element element = rootElement.element("contact");
        System.out.println(element.attributeValue("id"));

        System.out.println("==========================");
        //获取子元素下面所有子元素并获取他们的值

        List<Element> elementList = element.elements();
        for (Element element1 : elementList) {
            System.out.println(element1.getName()+"="+element1.getText());
        }


    }
}
